/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.close.hazard.controller;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.close.basic.dto.ClosedGeneralCarAuditDTO;
import com.hopes.bizz.close.common.constant.BasicDicConstant;
import com.hopes.bizz.close.common.constant.HazardDicConstant;
import com.hopes.bizz.close.hazard.dto.ClosedHazardCarAuditDTO;
import com.hopes.bizz.close.hazard.dto.ClosedHazardCarDTO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.close.hazard.entity.ClosedHazardCarEntity;
import com.hopes.bizz.close.hazard.dto.ClosedHazardCarExcelDTO;
import com.hopes.bizz.close.hazard.vo.ClosedHazardCarExcelVO;
import com.hopes.bizz.close.hazard.service.ClosedHazardCarService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 危险品运输车辆
 *
 * @author pig
 * @date 2023-10-24 19:40:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/hazardcar")
@Tag(description = "hazardcar", name = "危险品运输车辆管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ClosedHazardCarController {

    private final ClosedHazardCarService closedHazardCarService;

    /**
     * 分页查询
     *
     * @param page               分页对象
     * @param closedHazardCarDTO 危险品运输车辆
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('hazard_hazardcar_view')")
    public R getClosedHazardCarPage(Page page, ClosedHazardCarDTO closedHazardCarDTO) {
        LambdaQueryWrapper<ClosedHazardCarEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotNull(closedHazardCarDTO.getOrgId()), ClosedHazardCarEntity::getOrgId, closedHazardCarDTO.getOrgId());
        wrapper.eq(StrUtil.isNotBlank(closedHazardCarDTO.getUseFlag()), ClosedHazardCarEntity::getUseFlag, closedHazardCarDTO.getUseFlag());
        wrapper.eq(ObjUtil.isNotNull(closedHazardCarDTO.getHazardType()), ClosedHazardCarEntity::getHazardType, closedHazardCarDTO.getHazardType());
        wrapper.eq(ObjUtil.isNotNull(closedHazardCarDTO.getAuditStatus()), ClosedHazardCarEntity::getAuditStatus, closedHazardCarDTO.getAuditStatus());
        wrapper.eq(StrUtil.isNotBlank(closedHazardCarDTO.getBlacklistFlag()), ClosedHazardCarEntity::getBlacklistFlag, closedHazardCarDTO.getBlacklistFlag());
        wrapper.like(StrUtil.isNotBlank(closedHazardCarDTO.getKeyword()), ClosedHazardCarEntity::getLicencePlateNumber, closedHazardCarDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedHazardCarDTO.getKeyword()), ClosedHazardCarEntity::getDriverName, closedHazardCarDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedHazardCarDTO.getKeyword()), ClosedHazardCarEntity::getDriverPhone, closedHazardCarDTO.getKeyword());
        wrapper.orderByAsc(ClosedHazardCarEntity::getOrgId).orderByDesc(ClosedHazardCarEntity::getApplyTime);
        return R.ok(closedHazardCarService.page(page, wrapper));
    }

    /**
     * 分页查询（移动端）我的危险品车辆申请
     * @param page                     分页参数
     * @param closedHazardCarDTO       查询参数
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/selfPage")
    @PreAuthorize("@pms.hasPermission('hazard_hazardcar_view')" )
    public R getAppHazardCarSelfPage(Page page, ClosedHazardCarDTO closedHazardCarDTO) {
        return R.ok(closedHazardCarService.queryAppHazardCarSelfPage(page, closedHazardCarDTO));
    }

    /**
     * 分页查询（移动端）企业待审批的申请
     * @param page                     分页参数
     * @param closedHazardCarDTO       查询参数
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/audit/toDoPage")
    @PreAuthorize("@pms.hasPermission('hazard_hazardcar_view')" )
    public R getAppHazardCarAuditToDoPage(Page page, ClosedHazardCarDTO closedHazardCarDTO) {
        final List<Integer> auditStatusList = Collections.singletonList(HazardDicConstant.CLOSE_HAZARD_CAR_AUDIT_STATUS_NOT_AUDIT);
        return R.ok(closedHazardCarService.queryAppHazardCarAuditPage(page, auditStatusList, closedHazardCarDTO));
    }

    /**
     * 分页查询（移动端）企业审批完成的申请
     * @param page                     分页参数
     * @param closedHazardCarDTO       查询参数
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/audit/donePage")
    @PreAuthorize("@pms.hasPermission('hazard_hazardcar_view')" )
    public R getAppHazardCarAuditDonePage(Page page, ClosedHazardCarDTO closedHazardCarDTO) {
        final List<Integer> auditStatusList = Arrays.asList(
                HazardDicConstant.CLOSE_HAZARD_CAR_AUDIT_STATUS_PASS_AUDIT,
                HazardDicConstant.CLOSE_FLOW_BILL_AUDIT_STATUS_NOT_PASS_AUDIT);
        return R.ok(closedHazardCarService.queryAppHazardCarAuditPage(page, auditStatusList, closedHazardCarDTO));
    }


    /**
     * 通过id查询危险品运输车辆
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('hazard_hazardcar_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(closedHazardCarService.getById(id));
    }

    /**
     * 新增危险品运输车辆
     *
     * @param closedHazardCar 危险品运输车辆
     * @return R
     */
    @Operation(summary = "新增危险品运输车辆", description = "新增危险品运输车辆")
    @SysLog("新增危险品运输车辆")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('hazard_hazardcar_add')")
    public R save(@RequestBody ClosedHazardCarEntity closedHazardCar) {
        return R.ok(closedHazardCarService.save(closedHazardCar));
    }

    /**
     * 新增&&提交危险品运输车辆
     *
     * @param closedHazardCar 危险品运输车辆
     * @return R
     */
    @Operation(summary = "新增并提交危险品运输车辆", description = "新增并提交危险品运输车辆")
    @SysLog("新增并提交危险品运输车辆")
    @PostMapping("/saveAndSubmit")
    @PreAuthorize("@pms.hasPermission('hazard_hazardcar_add')")
    public R saveAndSubmit(@RequestBody ClosedHazardCarEntity closedHazardCar) {
        return R.ok(closedHazardCarService.saveAndSubmit(closedHazardCar));
    }

    /**
     * 修改危险品运输车辆
     *
     * @param closedHazardCar 危险品运输车辆
     * @return R
     */
    @Operation(summary = "修改危险品运输车辆", description = "修改危险品运输车辆")
    @SysLog("修改危险品运输车辆")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('hazard_hazardcar_edit')")
    public R updateById(@RequestBody ClosedHazardCarEntity closedHazardCar) {
        return R.ok(closedHazardCarService.updateById(closedHazardCar));
    }

    /**
     * 提交危险品车辆信息
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "提交危险品车辆信息", description = "提交危险品车辆信息")
    @SysLog("提交危险品车辆信息")
    @GetMapping("/submit/{id}")
    @PreAuthorize("@pms.hasPermission('hazard_hazardcar_edit')")
    public R submitById(@PathVariable("id") Long id) {
        return R.ok(closedHazardCarService.submitById(id));
    }

    /**
     * 审核危险品运输车辆
     *
     * @param closedGeneralCarAuditDTO 审核危险品运输车辆
     * @return R
     */
    @Operation(summary = "审核危险品运输车辆", description = "审核危险品运输车辆")
    @SysLog("审核普通车辆信息")
    @PutMapping("/audit")
    @PreAuthorize("@pms.hasPermission('hazard_hazardcar_audit')")
    public R auditById(@RequestBody ClosedHazardCarAuditDTO closedGeneralCarAuditDTO) {
        return R.ok(closedHazardCarService.auditById(closedGeneralCarAuditDTO));
    }

    /**
     * 通过id删除危险品运输车辆
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除危险品运输车辆", description = "通过id删除危险品运输车辆")
    @SysLog("通过id删除危险品运输车辆")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('hazard_hazardcar_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(closedHazardCarService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('hazard_hazardcar_export')")
    public List<ClosedHazardCarExcelVO> template() {
        return closedHazardCarService.exportExcel(Collections.singletonList(new ClosedHazardCarEntity()));
    }

    /**
     * 导出excel 表格
     *
     * @param closedHazardCarDTO 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hazard_hazardcar_export')")
    public List<ClosedHazardCarExcelVO> export(ClosedHazardCarDTO closedHazardCarDTO) {
        LambdaQueryWrapper<ClosedHazardCarEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotNull(closedHazardCarDTO.getOrgId()), ClosedHazardCarEntity::getOrgId, closedHazardCarDTO.getOrgId());
        wrapper.eq(StrUtil.isNotBlank(closedHazardCarDTO.getUseFlag()), ClosedHazardCarEntity::getUseFlag, closedHazardCarDTO.getUseFlag());
        wrapper.eq(ObjUtil.isNotNull(closedHazardCarDTO.getHazardType()), ClosedHazardCarEntity::getHazardType, closedHazardCarDTO.getHazardType());
        wrapper.eq(ObjUtil.isNotNull(closedHazardCarDTO.getAuditStatus()), ClosedHazardCarEntity::getAuditStatus, closedHazardCarDTO.getAuditStatus());
        wrapper.eq(StrUtil.isNotBlank(closedHazardCarDTO.getBlacklistFlag()), ClosedHazardCarEntity::getBlacklistFlag, closedHazardCarDTO.getBlacklistFlag());
        wrapper.like(StrUtil.isNotBlank(closedHazardCarDTO.getKeyword()), ClosedHazardCarEntity::getLicencePlateNumber, closedHazardCarDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedHazardCarDTO.getKeyword()), ClosedHazardCarEntity::getDriverName, closedHazardCarDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedHazardCarDTO.getKeyword()), ClosedHazardCarEntity::getDriverPhone, closedHazardCarDTO.getKeyword());
        return closedHazardCarService.exportExcel(closedHazardCarService.list(wrapper));
    }

    /**
     * 导入excel 表格
     *
     * @param closedHazardCarExcelDtoList 查询条件
     * @param bindingResult               错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('hazard_hazardcar_export')")
    public R importExcel(@RequestExcel List<ClosedHazardCarExcelDTO> closedHazardCarExcelDtoList, BindingResult bindingResult) {
        return closedHazardCarService.importExcel(closedHazardCarExcelDtoList, bindingResult);
    }
}
